import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12
import QtLocation 5.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0
import AIClient.module 1.0
import Result.module 1.0
import QtQuick.Dialogs 1.3

Window {
    modality: Qt.ApplicationModal
    title: "忘记密码"
    id: reset_passwd_dlg
    visible: false
    height: Screen.desktopAvailableHeight*0.3
    width: Screen.desktopAvailableWidth*0.3

    Image {
        anchors.fill: parent
        source: "qrc:/images/images/AIback.jpg"
    }
    TipMessage {
        id: tip
        width: parent.width
    }

    AIClient {
        id: client
    }

    function reset(email)
    {
        filedUsr.text = email
        filedPawd.text = ""
        filedPawd_accept.text = ""
        fieldValidate.text = ""
    }

    //用户输入
    Rectangle {
        width: 250
        height: 180
        color: "transparent"
        anchors.centerIn: parent

//                    anchors.bottom: parent.bottom
//                    anchors.bottomMargin: 100
//                    anchors.horizontalCenter: parent.horizontalCenter

        GridLayout {
            id: grid_reset
            rows: 5
            columns: 3
            anchors.fill: parent
            columnSpacing: 10
            Text {
                Layout.column: 0
                Layout.row: 0
                text: qsTr("邮箱")
                font.pointSize: 12
                font.bold: true
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                color: "orange"
            }
            TextField {
                Layout.column: 1
                Layout.row: 0
                id: filedUsr
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                text: "superadmin@aiplatform.com"
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: filedUsr
                }
            }

            Text {
                Layout.column: 0
                Layout.row: 1
                text: qsTr("验证码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }

            TextField {
                Layout.column: 1
                Layout.row: 1
                id: fieldValidate
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
//                echoMode: "Password"
//                passwordCharacter: "*"
                text: ""
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: fieldValidate
                }
            }

            Text {
                Layout.column: 0
                Layout.row: 2
                text: qsTr("输入新密码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }

            TextField {
                Layout.column: 1
                Layout.row: 2
                id: filedPawd
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                echoMode: "Password"
                passwordCharacter: "*"
                text: ""
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: filedPawd
                }
            }

            Text {
                Layout.column: 0
                Layout.row: 3
                text: qsTr("确认新密码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }

            TextField {
                Layout.column: 1
                Layout.row: 3
                id: filedPawd_accept
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                echoMode: "Password"
                passwordCharacter: "*"
                text: ""
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: filedPawd_accept
                }
            }

            Button {
                Layout.column: 2
                Layout.row: 1
                id: btn_forget_passwd
                text:"获取邮箱验证码"
                font.underline: true
                font.bold: btn_forget_passwd.hovered
                contentItem: Text {
                    text: btn_forget_passwd.text
                    font: btn_forget_passwd.font
                    opacity: enabled ? 1.0 : 0.3
                    color: "orange"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
                background: Rectangle{
                    anchors.fill: btn_forget_passwd
                    color: "transparent"
//                    Text {
//                        color: "orange"
//                        font.bold: btn_forget_passwd.hovered
//                        text: qsTr("获取邮箱验证码")
//                        anchors.fill: parent
//                    }
                }
//                onHoveredChanged: {
//                    if (hovered)
//                    {
//                        btn_forget_passwd.
//                    }
//                }

                onClicked: {
                    if (filedUsr.text.length <= 0 )
                    {
                        tip.showmsg("邮箱不得为空")
                    }

                    var res = client.get_reset_validate_code(filedUsr.text)
                    var jres = JSON.parse(res)
                    if ( jres["code"] === 0 )
                    {
                        tip.showmsg("获取验证码成功")
                    }
                    else
                    {
                        tip.showmsg(jres["message"])
                    }
                }
            }

            //登录按钮
            Rectangle {
                id: logBtn
                width: 150
                height: 33
                color: "#6facf9"
                radius: 4
                border.width: 1
                border.color: "black"
                Layout.column: 1
                Layout.row: 4
                Layout.fillWidth: true
                Text {
                    color: "#ffffff"
                    text: qsTr("提交")
                    anchors.centerIn: parent
                    font.pointSize: 12
                }
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
//                            anchors.horizontalCenter: parent.horizontalCenter
//                            anchors.top: grid.bottom
//                            anchors.topMargin: 40

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered: {
                        logBtn.color = "#333333"
                    }
                    onPressed: {
                        logBtn.color = "#111111"
                        if ( filedPawd.text.length <= 5 )
                        {
                            tip.showmsg("密码长度不得少于6！")
                            return
                        }

                        if ( filedPawd_accept.text.length <= 5 )
                        {
                            tip.showmsg("密码长度不得少于6！")
                            return
                        }

                        if ( filedUsr.text.length <= 0 )
                        {
                            tip.showmsg("邮箱不得为空！")
                            return
                        }

                        if ( fieldValidate.text.length <= 0 )
                        {
                            tip.showmsg("验证码不得为空！")
                            return
                        }


                        if ( filedPawd.text !== filedPawd_accept.text )
                        {
                            tip.showmsg("密码输入不一致！")
                            return
                        }

                        //var res = "{\"code\":0}"
                        //client.setConfig("")
                        var res = client.reset_password(filedUsr.text,fieldValidate.text,filedPawd.text)
                        var jres = JSON.parse(res)
                        if (jres["code"] === 0) {
                            tip.showmsg("修改密码成功")
                            delay(2000)

                        }
                        else
                        {
                            tip.showmsg(jres["message"])
                        }

                        //window.m_token = jres["token"]
                        //console.info(window.m_token)
                    }
                    onReleased: {
                        logBtn.color = "#333333"
                    }

                    onExited: {
                        logBtn.color = "#6facf9"
                    }
                }
            }
        }


    }

    Timer {
        id: timer
    }
    function delay(delayTime) {
        //var timer = new Timer();
        timer.interval = delayTime;
        timer.repeat = false;
        timer.triggered.connect(function () {
            reset_passwd_dlg.hide()
        })
        timer.start();
    }

}

